<template>
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false" width="600px" v-model="visible" @close="handleClose" title="编辑商品信息">
        <el-scrollbar class="ba-table-form-scrollbar" v-loading="loading">
            <el-form ref="formRef" :model="form" label-position="right" label-width="100px" :rules="rules" class="mt-8">
                <el-form-item label="标题" prop="title">
                    <el-input v-model="form.title"></el-input>
                </el-form-item>
                <el-form-item label="编号" prop="ean">
                    <el-input v-model="form.ean"></el-input>
                </el-form-item>
                <el-form-item label="分类" prop="category_ids">
                    <el-select v-model="form.category_ids" class="w-full" multiple
                        ><el-option v-for="item in categoryList" :value="item.id" :key="item.id" :label="item.name"></el-option
                    ></el-select>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" type="textarea" row="5"></el-input>
                </el-form-item>
            </el-form>
        </el-scrollbar>
        <template #footer>
            <el-button @click="handleClose">取消</el-button>
            <el-button type="primary" @click="handleSave"> 确定 </el-button>
        </template>
    </el-dialog>
</template>
<script setup>
import { watch, ref, reactive, nextTick } from 'vue'
import createAxios from '/@/utils/axios'
import { ElMessage } from 'element-plus'
import lo from 'lodash'
const props = defineProps({
    modelValue: Boolean,
    data: Object,
    categoryList: Array,
})
let visible = ref(false)
const rules = reactive({
    title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
})
let form = ref({}) // 表单数据
let formRef = ref(null) // 表单实例
let loading = ref(false) // 表单加载状态

watch(
    () => props.modelValue,
    (val) => {
        visible.value = val
        if (val) {
            nextTick(() => {
                formRef.value.resetFields()
                form.value = { ...props.data }
                form.value.category_ids = lo.map(form.value.category_ids, lo.unary(parseInt)) // 把数组里的字符串转成int类型
            })
        }
    }
)

const emits = defineEmits(['close', 'update'])

//关闭操作
const handleClose = () => {
    emits('close', false) // 可以更新 v-model 绑定的值
}

//保存操作
const handleSave = async () => {
    await formRef.value.validate(async (valid) => {
        if (valid) {
            loading.value = true
            let params = {
                id: form.value.id,
                title: form.value.title,
                ean: form.value.ean,
                category_ids: form.value.category_ids.join(','),
                remark: form.value.remark,
            }
            await createAxios({ url: '/admin/wms.Products/changeTitle', method: 'post', data: params })
                .then((res) => {
                    if (res.code === 1) {
                        ElMessage({ type: 'success', message: res.msg })
                        emits('update')
                        handleClose()
                    } else {
                        ElMessage({ type: 'error', message: res.msg })
                    }
                })
                .finally(() => {
                    loading.value = false
                })
        }
    })
}
</script>
<style scoped lang="scss"></style>
